package com.test.util;

import java.sql.Connection;
import java.sql.SQLException;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.test.Constants;

/**
 * JDBC+C3P0. <br>
 * 构建数据库连接池.
 * <p>
 * Copyright: Copyright (c) Nov 26, 2010 2:16:28 PM
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * 
 * @author suncg@c-platform.com
 * @version 1.0.0
 */
public final class DBUtils {

	private final static ThreadLocal<Connection> connection_holder = new ThreadLocal<Connection>();

	private static final ComboPooledDataSource source = new ComboPooledDataSource();

	static {
		try {
			source.setDriverClass((String) Constants.settings.get("driver"));
			source.setJdbcUrl((String) Constants.settings.get("url"));
			source.setUser((String) Constants.settings.get("user"));
			source.setPassword((String) Constants.settings.get("password"));
			source.setAutoCommitOnClose(false);
			source.setAcquireRetryAttempts(30);
			source.setAcquireRetryDelay(1000);
			source.setMaxStatements(180);
			source.setInitialPoolSize(45);
			source.setMaxPoolSize(100);
			source.setMinPoolSize(30);
			source.setMaxIdleTime(60);
			source.setNumHelperThreads(5);
			source.setIdleConnectionTestPeriod(60);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	private DBUtils() {
	}

	/**
	 * 关闭连接池
	 */
	public static void closeConnPoll() {
		if (source != null)
			source.close();
	}

	/**
	 * 获取连接池中连接
	 * 
	 * @return
	 */
	private static Connection getConnection() {
		try {
			Connection conn = source.getConnection();
			return conn;
		}
		catch (SQLException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	/**
	 * 获取当前连接
	 * 
	 * @return
	 */
	public static Connection getCurrentConnection() {
		Connection conn = connection_holder.get();
		if (conn == null) {
			connection_holder.set(getConnection());
		}
		return connection_holder.get();
	}

	/**
	 * 事务提交
	 */
	public static void commit() {
		Connection conn = getCurrentConnection();
		try {
			conn.commit();
		}
		catch (SQLException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	/**
	 * 关闭连接
	 * 
	 * @throws SQLException
	 */
	public static void closeConn() throws SQLException {
		Connection conn = getCurrentConnection();
		try {
			conn.close();
			connection_holder.set(null);
		}
		catch (SQLException e) {
			throw new SQLException(e.getMessage());
		}
	}

	/**
	 * 事务回滚
	 */
	public static void rollback() {
		Connection conn = getCurrentConnection();
		try {
			conn.rollback();
		}
		catch (SQLException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	/**
	 * 执行DML语句
	 * 
	 * @param sql
	 * @return
	 * @throws SQLException
	 */
	public static int executeDMQuery(String sql) throws SQLException {
		try {
			Connection conn = getCurrentConnection();
			return conn.createStatement().executeUpdate(sql);
		}
		catch (Exception e) {
			rollback();
			throw new SQLException(e.getMessage());
		}
		finally {
			commit();
		}
	}

	public static void main(String[] args) {

		/*
		 * try { QueryRunner qRunner = new QueryRunner(); BeanHandler<Bean>
		 * beanHandler = new BeanHandler<Bean>(Bean.class); Bean bean =
		 * qRunner.query(getCurrentConnection(), "select * from bean where id =
		 * ?", beanHandler, 6); System.out.println(bean.getName()); } catch
		 * (Exception e) { e.printStackTrace(); }
		 */
	}

}
